package com.dxl.common.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页模型
 * @author 邓小林
 * @param <T>
 */
public class Page<T> implements Serializable {
	private static final long serialVersionUID = 793242020698033370L;
	public static final int PAGE_NO = 1;
	public static final int PAGE_SIZE = 20;
	
	private int pageNo;
	private int pageSize;
	private long total;
	private List<T> rows;
	private List<Object> footer;
	
	public Page(int total) {
		this(PAGE_NO, PAGE_SIZE, total);
	}
	
	public Page(int pageSize, long total) {
		this(PAGE_NO, pageSize, total, null);
	}
	
	public Page(int pageNo, int pageSize, long total) {
		this(pageNo, pageSize, total, null);
	}
	
	public Page(int pageNo, int pageSize, long total, List<T> page) {
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.total = total;
		this.rows = page;
	}
	
	public long getPageCount() {
		return this.total == 0 ? 0 : this.total / this.pageSize + (this.total % this.pageSize == 0 ? 0 : 1);
	}
	
	public int getOffset() {
		return (this.pageNo - 1) * this.pageSize;
	}
	
	public int getPageNo() {
		return this.pageNo;
	}
	
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	
	public int getPageSize() {
		return this.pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public long getTotal() {
		return this.total;
	}
	
	public void setTotal(long total) {
		this.total = total;
	}
	
	public List<T> getRows() {
		return this.rows == null ? new ArrayList<T>() : this.rows;
	}
	
	public void setRows(List<T> rows) {
		this.rows = rows;
	}
	
	public boolean hasPre() {
		return this.pageNo > PAGE_NO;
	}
	
	public boolean hasNext() {
		return this.pageNo < this.getPageCount();
	}
	
	public static List<Integer[]> listPages(int pageSize, int totalCount) {
		List<Integer[]> rtnList = new ArrayList<Integer[]>();
		int pageCount = (totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize + 1));
		Integer[] pages;
		int k = 1, diff;
		for (int i = 0; i < pageCount; i++) {
			diff = totalCount - k + 1;
			if (diff < pageSize) {
				pages = new Integer[diff];
				for (int j = 0; j < diff; j++) {
					pages[j] = k++;
				}
			} else {
				pages = new Integer[pageSize];
				for (int j = 0; j < pageSize; j++) {
					pages[j] = k++;
				}
			}
			rtnList.add(pages);
		}
		return rtnList;
	}
	
	public static int pageCount(int pageSize, int totalCount) {
		if (totalCount <= 0) {
			return 0;
		}
		return (totalCount % pageSize == 0 ? totalCount / pageSize : (totalCount / pageSize + 1));
	}

	public List<Object> getFooter() {
		return footer;
	}

	public void setFooter(List<Object> footer) {
		this.footer = footer;
	}
}
